/*
 * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
 *
 * This software is distribuable under the GNU Lesser General Public License.
 * For more information visit gnu.org.
 */
package org.emergent.antbite.savant;


import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * This class is a single artifact produce by some project.
 * </p>
 *
 * @author  Brian Pontarelli
 */
public class Artifact {

    private String group;
    private String project;
    private String name;
    private String version;
    private String type;
    private int expireMinutes;
    private Date expireTime;
    private List dependencies = new ArrayList();


    public String getGroup() {
        return group;
    }

    public void setGroup(String group) {
        this.group = group;
    }

    public String getProjectname() {
        return project;
    }

    public void setProjectname(String project) {
        this.project = project;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getExpireminutes() {
        return expireMinutes;
    }

    public void setExpireminutes(int expireMinutes) {
        this.expireMinutes = expireMinutes;
    }

    public Date getExpiretime() {
        return expireTime;
    }

    public void setExpiretime(Date expireTime) {
        this.expireTime = expireTime;
    }

    /**
     * Returns the list of dependencies that this single Artifact has. This List
     * is immutable.
     */
    public List getDependencies() {
        return Collections.unmodifiableList(dependencies);
    }

    /**
     * Adds a new dependency Artifact for this Artifact.
     */
    public void addDependency(Artifact artifact) {
        dependencies.add(artifact);
    }

    /**
     * <p>
     * Returns a directory where this artifact can be found or stored. This is
     * composed of the group namd and the project name like this:
     * </p>
     *
     * <p>
     * group/project
     * </p>
     *
     * @return  The directory
     */
    public String getArtifactDir() {
        return group + File.separator + project;
    }

    /**
     * <p>
     * Returns a file path (including the directory that is retrieve from {@link
     * #getArtifactDir()}) where this artifact can be found or stored. This is
     * composed of the group name, the project name, artifact name, version and
     * type like this:
     * </p>
     *
     * <p>
     * group/project/artifact_name-version.type
     * </p>
     *
     * @return  The path
     */
    public String getArtifactFile() {
        return getArtifactDir() + File.separator + name + "-" + version + "." +
            type;
    }

    /**
     * <p>
     * Returns a file path (including the directory that is retrieve from {@link
     * #getArtifactDir()}) where the dependency XML for this artifact can be
     * found or stored. This is composed of the group name, the project name,
     * artifact name, version and type like this:
     * </p>
     *
     * <p>
     * group/project/artifact_name-version.type.deps
     * </p>
     *
     * @return  The path
     */
    public String getArtifactDepsFile() {
        return getArtifactFile() + ".deps";
    }

    /**
     * Converts this artifact to a String.
     *
     * @return  To String
     */
    public String toString() {
        StringBuffer buf = new StringBuffer();
        buf.append(group).append(", ").append(project).append(", ").append(name);
        if (version != null) {
            buf.append("-").append(version);
        }
        buf.append(".").append(type);

        return buf.toString();
    }

    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (!(o instanceof Artifact)) {
            return false;
        }

        final Artifact artifact = (Artifact) o;

        return (group == artifact.group || (group != null && group.equals(artifact.group))) &&
            (name == artifact.name || (name != null && name.equals(artifact.name))) &&
            (project == artifact.project || (project != null && project.equals(artifact.project))) &&
            (type == artifact.type || (type != null && type.equals(artifact.type))) &&
            (version == artifact.version || (version != null && version.equals(artifact.version)));
    }

    public int hashCode() {
        int result;
        result = (group != null ? group.hashCode() : 0);
        result = 29 * result + (project != null ? project.hashCode() : 0);
        result = 29 * result + (name != null ? name.hashCode() : 0);
        result = 29 * result + (version != null ? version.hashCode() : 0);
        result = 29 * result + (type != null ? type.hashCode() : 0);
        return result;
    }

    /**
     * Validates the attributes of the artifact.
     *
     * @throws  SavantException If group, project, name or type is null
     */
    public void validate() throws SavantException {
        if (group == null || project == null || name == null || type == null) {
            throw new SavantException("A group, project, name and type are " +
                "required attributes for artifacts");
        }
    }
}